<?php
Class AnnounceAction extends BaseAction{
   /*
    +----------------------------------------------------------
    * 公告管理
    +----------------------------------------------------------
    */
	public function index(){
		
		$news = D('News')->select();
		$this->assign('news',$news);
		$this->display();
	}
	
	public function add(){
		//显示公告并保存到数据库中
		$this->assign('select','add');
		$this->display('index');
	}
	
	public function adds(){
		$data['title'] = $_POST['title'];
		$data['content'] = trim($_POST['content']);
		empty($data['title']) && $this->error('请填写标题!');
		empty($data['content']) && $this->error('公告内容不能为空!');
		if($_POST['status']) $data['status'] = $_POST['status'];
		$data['create_time'] = time();
		$data['update_time'] = time();
		
		$news = D('News')->data($data)->add();
		if($news) 
			$this->success('添加成功!');
		else $this->error('操作失败，请重试!');
	}
	
	public function edit(){
		//编辑公告内容  
		$nid = $_GET['news_id'];
		$news = D('News')->where('news_id ='.$nid)->find();
		$this->assign('news',$news);
		$this->assign('select','edit');
		$this->display('index');
	}
	
	public function edits(){
		$data['news_id'] = $_POST['id'];
		$data['title'] = $_POST['title'];
		$data['content'] = $_POST['content'];
		$data['update_time'] = time();
		empty($data['title']) && $this->error('请填写标题!');
		empty($data['content']) && $this->error('公告内容不能为空!');
		
		D('News')->data($data)->save();
		$this->success('修改成功!');
	}
	
	public function delete(){
		//删除数据库中的公告记录
		$checkbox = $_POST['id'];
		if(!empty($checkbox)){
			foreach($checkbox as $id){
				$news = D('News')->delete($id);
			}
			if($news)
				$this->success('删除成功!');
			else $this->error('操作失败，请重试!');
		}
	}
	
	public function publish(){
		if(!empty ($_GET['hidden_id'])){
			$data['news_id'] = $_GET['hidden_id'];
			$result = D('News')->where($data)->setField('status',1);
			if($result){
				$this->success('操作成功!');
			}else{
				$this->error('操作失败!');
			}
		}elseif(!empty ($_GET['publish_id'])){
			$data['news_id'] = $_GET['publish_id'];
			$result = D('News')->where($data)->setField('status',0);
			if($result){
				$this->success('操作成功!');
			}else{
				$this->error('操作失败!');
			}
		}else{
			$this->error('公告不存在!');
		}
		
	}

}
?>
